Graphic Design with ggplot2

Konsep Dasar {ggplot2}:
Data, Aesthetics, and Layers

Setup

library ggplot2


… merupakan paket R untuk membuat visualisasi data, dikembangkan oleh Hadley Wickham pada tahun 2005

# install.packages("ggplot2")
library(ggplot2)


… adalah bagian dari ekosistem {tidyverse}, kumpulan paket R untuk analisis data yang terintegrasi

# install.packages("tidyverse")
library(tidyverse)

The Grammar of {ggplot2}

The Grammar of {ggplot2}


Komponen Fungsi Keterangan
Data ggplot(data)          Data mentah yang ingin Anda visualisasikan
Aesthetics           aes() Pemetaan antara variabel dan properti visual (seperti warna, ukuran, posisi)
Geometries geom_*() Bentuk geometris yang merepresentasikan data (seperti titik, garis, batang, dll.)

The Grammar of {ggplot2}


Component Function Explanation
Data ggplot(data)          Data mentah yang ingin Anda visualisasikan
Aesthetics           aes() Pemetaan antara variabel dan properti visual (seperti warna, ukuran, posisi)
Geometries geom_*() Bentuk geometris yang merepresentasikan data (seperti titik, garis, batang, dll.)
Statistics stat_*() Transformasi statistik yang diterapkan pada data
Scales scale_*() Pemetaan antara data dan dimensi estetika.
Coordinate System coord_*() Menempatkan data ke dalam bidang grafik
Facets facet_*() Pengaturan data ke dalam grid beberapa grafik
Visual Themes theme() / theme_*() Pengaturan visual default dari sebuah grafik

Contoh Sederhana Menggunakan ggplot

Data

Jumlah Penyewaan Sepeda di London, UK, didukung oleh TfL Open Data

  • Mencakup tahun 2015 dan 2016
  • Termasuk data cuaca yang diperoleh dari freemeteo.com
  • Disiapkan oleh Hristo Mavrodiev untuk Kaggle


bikes <- readr::read_csv(
  here::here("slides_ggplot2", "data", "london-bikes-custom.csv"),
  ## atau: "https://raw.githubusercontent.com/dedenistiawan/Dataset/refs/heads/main/london-bikes-custom.csv"
  col_types = "Dcfffilllddddc"
)

bikes$season <- forcats::fct_inorder(bikes$season)
Variable Description Class
date Date encoded as `YYYY-MM-DD` date
day_night `day` (6:00am–5:59pm) or `night` (6:00pm–5:59am) character
year `2015` or `2016` factor
month `1` (January) to `12` (December) factor
season `winter`, `spring`, `summer`, or `autumn` factor
count Sum of reported bikes rented integer
is_workday `TRUE` being Monday to Friday and no bank holiday logical
is_weekend `TRUE` being Saturday or Sunday logical
is_holiday `TRUE` being a bank holiday in the UK logical
temp Average air temperature (°C) double
temp_feel Average feels like temperature (°C) double
humidity Average air humidity (%) double
wind_speed Average wind speed (km/h) double
weather_type Most common weather type character

ggplot2::ggplot()

The help page of the ggplot() function.

Data

ggplot(data = bikes)

Pemetaan Estetika


= menghubungkan variabel ke properti grafik

  • posisi (x, y)
  • warna (color, fill)
  • bentuk (shape, linetype)
  • ukuran (size)
  • transparansi (alpha)
  • pengelompokan (group)

Aesthetic Mapping

ggplot(data = bikes) +
  aes(x = temp_feel, y = count)

aesthetics

aes() di luar sebagai komponen

ggplot(data = bikes) +
  aes(x = temp_feel, y = count)


aes() di dalam, pemetaan variabel secara eksplisit

ggplot(data = bikes, mapping = aes(x = temp_feel, y = count))


aes() di dalam, pemetaan variabel secara implisit

ggplot(bikes, aes(temp_feel, count))


aes() di dalam, pemetaan variabel campuran

ggplot(bikes, aes(x = temp_feel, y = count))

Geometrical Layers

Geometries


= memahami estetika sebagai cara variabel ditampilkan secara visual

  • points
  • lines
  • polygons
  • text labels

Geometries

ggplot(
    bikes,
    aes(x = temp_feel, y = count)
  ) +
  geom_point()

Karakteristik Visual Setiap Lapisan Grafik

ggplot(
    bikes,
    aes(x = temp_feel, y = count)
  ) +
  geom_point(
    color = "#28a87d",
    alpha = .5,
    shape = "X",
    size = 4
  )

Setting vs Mapping of Visual Properties

ggplot(
    bikes,
    aes(x = temp_feel, y = count)
  ) +
  geom_point(
    color = "#28a87d",
    alpha = .5
  )

Setting vs Mapping of Visual Properties

ggplot(
    bikes,
    aes(x = temp_feel, y = count)
  ) +
  geom_point(
    aes(color = season),
    alpha = .5
  )

Mapping Expressions

ggplot(
    bikes,
    aes(x = temp_feel, y = count)
  ) +
  geom_point(
    aes(color = temp_feel > 20),
    alpha = .5
  )

latihan!

  • Buat grafik sebar (scatter plot) antara temp_feel dan temp.
    • Atur warna titik berdasarkan kondisi cuaca cerah (clear weather).
    • Atur ukuran titik berdasarkan nilai count.
    • Ubah bentuk titik menjadi belah ketupat (diamond).

Mapping Expressions

ggplot(
    bikes,
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear"),
    alpha = .5,
    size = 2
  )

Mapping to Size

ggplot(
    bikes,
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear",
        size = count),
    alpha = .5
  )

Setting a Constant Property

ggplot(
    bikes,
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear",
        size = count),
    shape = 18,
    alpha = .5
  )

Setting a Constant Property

ggplot(
    bikes,
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear",
        size = count),
    shape = 5,
    alpha = .5
  )

Setting a Constant Property

ggplot(
    bikes,
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear",
        size = count),
    shape = 9,
    alpha = .5
  )

Setting a Constant Property

ggplot(
    bikes,
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear",
        size = count),
    shape = 23,
    alpha = .5
  )

An overview of a set of available shapes, ordered by their type of shape (e.g. points, triangles etc).

Source: Albert’s Blog

Filter Data

ggplot(
    filter(bikes, !is.na(weather_type)),
    aes(x = temp, y = temp_feel)
  ) +
  geom_point(
    aes(color = weather_type == "clear",
        size = count),
    shape = 18,
    alpha = .5
  )

Local vs. Global Encoding

ggplot(
    bikes,
    aes(x = temp_feel, y = count)
  ) +
  geom_point(
    aes(color = season),
    alpha = .5
  )

Local vs. Global Encoding

ggplot(
    bikes,
    aes(x = temp_feel, y = count,
        color = season)
  ) +
  geom_point(
    alpha = .5
  )